package com.example.demo.service;


import com.example.demo.domain.Student;
import com.example.demo.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentService {

    @Autowired
    private StudentRepository studentRepository;

    public Map sava(Student student) {
        Map map = new HashMap();
        if (StringUtils.hasText(student.getNumber()) && StringUtils.hasText(student.getPassword())) {
            Student student1 = this.studentRepository.findByNumberAndPassword(student.getNumber(), student.getPassword());
            if (student1 == null) {
                map.put("msg", "成功");
                this.studentRepository.save(student);
            } else
                map.put("msg", "失败");

        } else
            map.put("msg", "参数不能为空");
        return map;
    }

    public Map login(String number, String password) {
        Map m = new HashMap();
    Student student = this.studentRepository.findByNumber(number);
        if (StringUtils.hasText(number) && StringUtils.hasText(password)) {
        if (student != null) {
            Student s = this.studentRepository.findByNumberAndPassword(number, password);
            if (s != null) {
                m.put("msg", "登陆成功");
            } else
                m.put("msg", "密码错误！");
        } else
            m.put("msg", "请注册");
    }else
            m.put("msg","参数错误");
        return m;
}

public List<Student> findall(){
        return this.studentRepository.findAll();
  }
}
